package com.raising.modules.cms.controller;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.cms.entity.Category;
import com.raising.modules.cms.service.CategoryService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 栏目表 控制器
 * @author gaoy
 * @createTime 2017-12-25 17:09:58
 */
@Controller
@RequestMapping("${adminPath}/cms/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;
    /**
     * 获取单个对象（共用）
     * @author gaoy
     * @createTime 2017-12-25 17:09:58
     */
    @ModelAttribute
    public Category get(@RequestParam(required = false) String id) {
        Category entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = categoryService.get(id);
        }
        if (entity == null) {
            entity = new Category();
        }
        return entity;
    }
    /**
    * 跳转至列表页面
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("cms:category:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "cms/category/list";
    }

    /**
    * 加载列表分页数据
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("sys:role:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Model model, Category category,Page<Category> page) {
        page.setEntity(category);
        page.setOrderBy(" a.id asc ");
        return new ResultVo(ResultVo.SUCCESS,"ok",categoryService.getPage(page));
    }

    /**
    * 跳转新增页面
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("cms:category:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Category category,Model model) {
        model.addAttribute("category", category);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "cms/category/add";
    }

    /**
    * 执行新增操作
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("cms:category:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Category category, RedirectAttributes redirectAttributes) {
        categoryService.save(category);
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/cms/category/update?id="+category.getId();
    }
    /**
    * 跳转更新页面
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("cms:category:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Category category, Model model) {
        model.addAttribute("category", category);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "cms/category/edit";
    }
    /**
    * 执行更新操作
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("cms:category:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Category category, RedirectAttributes redirectAttributes) {
        categoryService.save(category);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/cms/category/update?id="+category.getId();
    }
    /**
    * 执行删除操作
    * @author gaoy
    * @createTime 2017-12-25 17:09:58
    */
    @RequiresPermissions("cms:category:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        categoryService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/cms/category/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
